// Copyright (C) 2022 Huawei Device Co., Ltd.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package controllers

import (
	"dist/model"
	"dist/services"
	"dist/utils"
	"fmt"
	"net/http"
)

func Record(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPost {
		utils.Resp(w)(0, "success", map[string]interface{}{})
		return
	}
	var req model.RecordReq
	error := utils.RequestToStruct(r, &req)
	if error == nil {
		error = services.HandleRecords(req)
	}
	if error == nil {
		utils.Resp(w)(0, "success", map[string]interface{}{})
	} else {
		utils.Resp(w)(-1, "error", map[string]interface{}{})
	}
}

// 页面访问分布
func PageDistribution(w http.ResponseWriter, r *http.Request) {
	var req model.TimeRangeReq
	err := utils.RequestToStruct(r, &req)
	error := utils.CheckParameters(&req)
	if error != nil {
		utils.Resp(w)(-1, fmt.Sprintf("%v", error), map[string]interface{}{})
		return
	}
	if err == nil {
		pages := services.PageDistributionService(utils.TsToTime(req.StartTime), utils.TsToTime(req.EndTime))
		if pages == nil {
			utils.Resp(w)(0, "data error", map[string]interface{}{
				"total": 0,
				"pages": pages,
			})
		} else {
			var total int32 = 0
			for _, page := range pages {
				total += page.AccessCount
			}
			utils.Resp(w)(0, "success", map[string]interface{}{
				"total": total,
				"pages": pages,
			})
		}
	} else {
		utils.Resp(w)(-1, "param error", map[string]interface{}{})
	}

}

func PageUseRecord(w http.ResponseWriter, r *http.Request) {
	var req model.PageUseReq
	err := utils.RequestToStruct(r, &req)
	error := utils.CheckParameters(&req)
	if error != nil {
		utils.Resp(w)(-1, fmt.Sprintf("%v", error), map[string]interface{}{})
		return
	}
	if err == nil {
		useCount := services.PageUse(utils.TsToTime(req.StartTime), utils.TsToTime(req.EndTime), req.Interval, req.Action)
		if useCount == nil {
			utils.Resp(w)(0, "data error", useCount)
		} else {
			utils.Resp(w)(0, "success", useCount)
		}
		return
	} else {
		utils.Resp(w)(-1, "param error", map[string]interface{}{})
	}
}

func DownLoadUseRecord(w http.ResponseWriter, r *http.Request) {
	var req model.PageUseReq
	err := utils.RequestToStruct(r, &req)
	error := utils.CheckParameters(&req)
	if error != nil {
		utils.Resp(w)(-1, fmt.Sprintf("%v", error), map[string]interface{}{})
		return
	}
	if err == nil {
		useCount := services.DownloadUse(utils.TsToTime(req.StartTime), utils.TsToTime(req.EndTime), req.Interval)
		utils.Resp(w)(0, "success", useCount)
		return
	} else {
		utils.Resp(w)(-1, "param error", map[string]interface{}{})
	}
}

func PluginUsageStatistics(w http.ResponseWriter, r *http.Request) {
	var req model.TimeRangeReq
	err := utils.RequestToStruct(r, &req)
	error := utils.CheckParameters(&req)
	if error != nil {
		utils.Resp(w)(-1, fmt.Sprintf("%v", error), map[string]interface{}{})
		return
	}
	if err == nil {
		pluginData := services.PluginsUse(utils.TsToTime(req.StartTime), utils.TsToTime(req.EndTime))
		if pluginData == nil {
			utils.Resp(w)(0, "data error", map[string]interface{}{
				"total":      0,
				"pluginData": pluginData,
			})
		} else {
			var total int32 = 0
			for _, plugin := range pluginData {
				total += plugin.UseCount
			}
			utils.Resp(w)(0, "success", map[string]interface{}{
				"total":      total,
				"pluginData": pluginData,
			})
		}
	} else {
		utils.Resp(w)(-1, "param error", map[string]interface{}{})
	}
}

func CommandUseRecord(w http.ResponseWriter, r *http.Request) {
	var req model.TimeRangeWithIntervalReq
	err := utils.RequestToStruct(r, &req)
	error := utils.CheckParameters(&req)
	if error != nil {
		utils.Resp(w)(-1, fmt.Sprintf("%v", error), map[string]interface{}{})
		return
	}
	if err == nil {
		cmdData := services.CmdUse(utils.TsToTime(req.StartTime), utils.TsToTime(req.EndTime), req.Interval)
		if cmdData.Offline == nil || cmdData.Online == nil {
			utils.Resp(w)(0, "data error", map[string]interface{}{
				"offline": cmdData.Offline,
				"online":  cmdData.Online,
			})
		} else {
			utils.Resp(w)(0, "success", map[string]interface{}{
				"offline": cmdData.Offline,
				"online":  cmdData.Online,
			})
		}
	} else {
		utils.Resp(w)(-1, "param error", map[string]interface{}{})
	}
}

func RowRecord(w http.ResponseWriter, r *http.Request) {
	var req model.TimeRangeWithIntervalReq
	error := utils.RequestToStruct(r, &req)
	err := utils.CheckParameters(&req)
	if err != nil {
		utils.Resp(w)(-1, fmt.Sprintf("%v", err), map[string]interface{}{})
		return
	}
	if error == nil {
		datas := services.GetRowAndTabCount(utils.TsToTime(req.StartTime), utils.TsToTime(req.EndTime), req.Interval)
		if datas != nil {
			utils.Resp(w)(0, "success", datas)
		} else {
			utils.Resp(w)(0, "data error", datas)
		}
	} else {
		utils.Resp(w)(-1, "param error", map[string]interface{}{})
	}
}

// trace导入次数统计
func ImportTrace(w http.ResponseWriter, r *http.Request) {
	var req model.TimeRangeWithIntervalReq
	err := utils.RequestToStruct(r, &req)
	error := utils.CheckParameters(&req)
	if error != nil {
		utils.Resp(w)(-1, fmt.Sprintf("%v", error), map[string]interface{}{})
		return
	}
	if err == nil {
		pages := services.ImportTraceService(utils.TsToTime(req.StartTime), utils.TsToTime(req.EndTime), req.Interval)
		if pages == nil {
			utils.Resp(w)(0, "data error", map[string]interface{}{
				"importTrace": pages,
			})
		} else {
			utils.Resp(w)(0, "success", map[string]interface{}{
				"importTrace": pages,
			})
		}
	} else {
		utils.Resp(w)(-1, "param error", map[string]interface{}{})
	}
}

func RowSubRecord(w http.ResponseWriter, r *http.Request) {
	var req model.TimeRangeWithIntervalReq
	error := utils.RequestToStruct(r, &req)
	err := utils.CheckParameters(&req)
	if err != nil {
		utils.Resp(w)(-1, fmt.Sprintf("%v", err), map[string]interface{}{})
		return
	}
	if error == nil {
		datas := services.GetRowAndTabSubCount(utils.TsToTime(req.StartTime), utils.TsToTime(req.EndTime), req.Interval)
		if datas != nil {
			utils.Resp(w)(0, "success", datas)
		} else {
			utils.Resp(w)(0, "data error", datas)
		}
	} else {
		utils.Resp(w)(-1, "param error", map[string]interface{}{})
	}
}
